<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架商品
if($_RQ['op']=='list'){
	$params=array('type <>'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','scate','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：需要先基础设置--多语言开启--开启店铺语言（至少一个）,这里才显示内容！','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'image','title'=>'商品图片','name'=>'image'),
			array('type'=>'string','title'=>'商品名称','name'=>'title'),
			array('type'=>'string','title'=>'库存','name'=>'total'),
			array('name'=>'price','title'=>'价格','type'=>'string','set'=>'set'),
		),'action'=>array(
			array('type'=>'href','op'=>'edit','title'=>'翻译'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'商品名称'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$isset=$this->pdo_get('goods',array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'title'=>$post['title'],
			'diy'=>ijson_encode($post['diy'])
		);
		if($isset){
			$this->pdo_update('goods',$data,array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['fromid']=$id;
			$this->pdo_insert('goods',$data);
		}
		exi('设置成功');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择商品','error');	
	}
	$item=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$isset=$this->pdo_get('goods',array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['diy']=json_decode($isset['diy'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'商品内容',
		'edit_tab'=>true,
		'edits'=>array(),
	);
	$list=array(
		array('name'=>'title','tip'=>'商品名称','type'=>'stringdiy'),
		array('name'=>'subtitle','tip'=>'副标题','type'=>'stringdiy'),
		array('name'=>'shorttitle','tip'=>'短标题','type'=>'stringdiy'),
		array('name'=>'content','tip'=>'详情','type'=>'textdiy'),
	);
	$is_langs=str_array($this->setting['is_langs']);
	foreach($is_langs as $en){
		$one=array();
		$one['title']=$this->langs[$en];
		$one['list']=array();
		$one['list'][]=array('type'=>'jsup','title'=>'一键翻译本页内容','action'=>'jy_lang_weishop/index/upfanyi/en='.$en,'message'=>'确定要一键翻译吗？这样将覆盖原有设置','btntitle'=>'一键翻译');
		foreach($list as $fv){
			$fv['title']=$item[$fv['name']];
			$fv['name']='diy.'.$en.'_'.$fv['name'];
			$one['list'][]=$fv;
		}
		$htmls['edits'][]=$one;
	}
	if(!count($is_langs)){
		exi('基础设置--多语言开启--开启店铺语言（至少一个）,这里才显示内容','error');
	}
	$return['item']['fanyidata']=array();
	foreach($list as $k=>$fv){
		$return['item']['fanyidata']['_'.$fv['name']]=$fv['title'];
	}
	$return['htmls']=$htmls;
	exi($return);
}